package com.deng.mapper;

import com.deng.model.Comments;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 邓小熙
 * @Date 2022/5/13 10:01
 * @Version 1.0
 */
@Mapper
@Component
public interface CommentsMapper {
    /**
     * 首页评论推荐 limit 5
     * @return
     */

    @Select("SELECT * FROM comments ORDER BY comments_time LIMIT #{pageNum},#{limit}")
    List<Comments> findAllCommentsOrderByTime(@Param("pageNum") int pageNum, @Param("limit") int limit);

    /**
     * 通过文章ID查询评论数量
     * @param id
     * @return
     */
    @Select("SELECT COUNT(*) FROM paper,comments WHERE paper.id = comments.comments_paper AND paper.id = #{id}")
    int findCommentCountByPaperId(int id);

    /**
     * 通过文章ID查找对应文章的所有评论
     * @param id
     * @return
     */
    @Select("SELECT c.* FROM social_blog.comments c LEFT JOIN social_blog.paper p ON c.comments_paper = p.id WHERE comments_paper=#{id}")
    List<Comments> findAllCommentsByPaperId(int id);
    /**
     * 根据user查询近期的所有评论
     * @param userId
     * @return
     */
    @Select("SELECT * FROM social_blog.comments WHERE comments_user = #{userId} ORDER BY comments_time limit #{pageSize}")
//    @Select("SELECT * FROM social_blog.comments WHERE comments_user = #{userId} ORDER BY comments_time DESC LIMIT #{pageNum},#{pageSize}")
    List<Comments> findCommentsByUserId(@Param("userId") int userId,@Param("pageSize") int pageSize);
//    List<Comments> findCommentsByUserId(@Param("userId") int userId, @Param("pageNum") int pageNum, @Param("pageSize") int pageSize);
    /**
     * 增加评论
     * @param comments
     * @return
     */
    @Insert("INSERT INTO social_blog.comments(comments_content,comments_time,comments_paper,comments_user)" +
            "VALUES(#{comments.comments_content},#{comments.comments_time},#{comments.comments_paper},#{comments.comments_user})")
    int insertComments(@Param("comments") Comments comments);

    /**
     * 删除评论
     * @param id
     * @return
     */
    @Delete("DELETE FROM social_blog.comments WHERE id=#{id}")
    int deleteCommentById(int id);
}
